package com.mcmaster.android.menus;

import java.io.FileNotFoundException;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;

import com.google.android.maps.MapView;
import com.mcmaster.android.Output;
import com.mcmaster.android.R;
import com.mcmaster.android.control.GuidedTourControl;
import com.mcmaster.android.data.Tour;
import com.mcmaster.android.data.TourDatabase;

public class GuidedToursMenu extends Activity {

	Spinner tour_list_spinner_;
	List<String> tour_list_;
	String selected_tour_name_;
	
	public void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
        setContentView(R.layout.guided_layout_2);
        
        Button tours_tour_btn 	= (Button)findViewById(R.id.guided_tours_sel);
        Button tours_return_btn = (Button)findViewById(R.id.guided_return_btn);
        
        tours_tour_btn.setOnClickListener(new ButtonHandlerTour());
        tours_return_btn.setOnClickListener(new ButtonHandlerMainMenu());
        
        try {
			tour_list_ = TourDatabase.getTourNames();
			
	        Spinner tour_list_spinner_ = (Spinner) findViewById(R.id.tourlist);
	        ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tour_list_);
	        
	        spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
	        tour_list_spinner_.setAdapter(spinnerArrayAdapter);
	        tour_list_spinner_.setOnItemSelectedListener(new ItemSelectListener());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
        

	}
	
	private class ButtonHandlerTour implements View.OnClickListener {
	    public void onClick(View v) {
	    	try {
				handleTourClick();
			} catch (FileNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
	    private void handleTourClick() throws FileNotFoundException
		{
			if (selected_tour_name_ != null) {
				Tour t = TourDatabase.getTourFromName(selected_tour_name_);
				Intent i = new Intent(GuidedToursMenu.this, GuidedMenu.class);
				
				i.putExtra("name", selected_tour_name_);
				startActivity(i);
			}
			
		}
	}
	
	
	public class ItemSelectListener implements OnItemSelectedListener {
		
		public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
			selected_tour_name_ = parent.getItemAtPosition(pos).toString();
		}
		
		public void onNothingSelected(AdapterView parent) {
		}
	}
	
	private class ButtonHandlerMainMenu implements View.OnClickListener {
	    public void onClick(View v) {
	    	handleHomeClick();
	    }
	    private void handleHomeClick() {
			finish();
		}
	}
	
	
	
}
